package com.thenie.seckill.controller;


import com.thenie.seckill.pojo.User;
import com.thenie.seckill.service.IGoodsService;
import com.thenie.seckill.util.JsonUtil;
import com.thenie.seckill.validator.CurrentUser;
import com.thenie.seckill.vo.DetailVo;
import com.thenie.seckill.vo.GoodsVo;
import com.thenie.seckill.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @RequestMapping("/toList")
    public String toList(Model model, @CurrentUser User user){
        model.addAttribute("user",user);
        //从缓存中拿列表

        List<GoodsVo> goodsList = null;
        Object tem_list = redisTemplate.opsForValue().get("goodsList");
        if (tem_list!=null){
            //如果缓存中存在
            goodsList = JsonUtil.jsonToList((String)tem_list, GoodsVo.class);
        }else{
            goodsList = goodsService.findGoodVo();
            //也就说现在缓存中没有列表，进行添加
            redisTemplate.opsForValue().set("goodsList",JsonUtil.object2JsonStr(goodsList),60, TimeUnit.SECONDS);
        }
        model.addAttribute("goodsList",goodsList);
        return "goodsList";
    }

    /**
     * 商品详情页面
     * @param model
     * @param user
     * @param goodsId
     * @return
     */
    @RequestMapping("/toDetail/{goodsId}")
    public String toDetail(Model model,@CurrentUser User user,@PathVariable Long goodsId){
        //存储user域
        model.addAttribute("user", user);
        GoodsVo goods = null;
        //从缓存中拿商品详情
        Object tem_goods = redisTemplate.opsForValue().get("goodsDetail"+goodsId);
        if (tem_goods!=null){
            goods = JsonUtil.jsonStr2Object((String) tem_goods, GoodsVo.class);
        }else{
            goods = goodsService.findGoodsVoById(goodsId);
            redisTemplate.opsForValue().set("goodsDetail:"+goodsId,JsonUtil.object2JsonStr(goods),60, TimeUnit.SECONDS);
        }
        //存储秒杀商品详情
        model.addAttribute("goods", goods);
        Date startDate = goods.getStartDate();
        Date endDate = goods.getEndDate();
        Date nowDate = new Date();
        //秒杀状态
        int secKillStatus = 0;
        //剩余开始时间
        int remainSeconds = 0;
        //秒杀还未开始
        if (nowDate.before(startDate)) {
            remainSeconds = (int) ((startDate.getTime()-nowDate.getTime())/1000);
        // 秒杀已结束
        } else if (nowDate.after(endDate)) {
            secKillStatus = 2;
            remainSeconds = -1;
        // 秒杀中
        } else {
            secKillStatus = 1;
            remainSeconds = 0;
        }
        //存储秒杀状态
        model.addAttribute("secKillStatus",secKillStatus);
        //存储倒计时
        model.addAttribute("remainSeconds",remainSeconds);
        return "goodsDetail";
    }

    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public RespBean toDetail(HttpServletRequest request, HttpServletResponse
            response, Model model,@CurrentUser User user, @PathVariable Long goodsId) {
        GoodsVo goods = goodsService.findGoodsVoById(goodsId);
        Date startDate = goods.getStartDate();
        Date endDate = goods.getEndDate();
        Date nowDate = new Date();
        //秒杀状态
        int secKillStatus = 0;
        //剩余开始时间
        int remainSeconds = 0;
        //秒杀还未开始
        if (nowDate.before(startDate)) {
            remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
        // 秒杀已结束
        } else if (nowDate.after(endDate)) {
            secKillStatus = 2;
            remainSeconds = -1;
        // 秒杀中
        } else {
            secKillStatus = 1;
            remainSeconds = 0;
        }
        DetailVo detailVo = new DetailVo();
        detailVo.setGoodsVo(goods);
        detailVo.setUser(user);
        detailVo.setRemainSeconds(remainSeconds);
        detailVo.setSecKillStatus(secKillStatus);
        return RespBean.success(detailVo);
    }
}
